﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using System.Data.SqlClient;

namespace LotteryVote.Data
{
    public class PlayWayDataAccess : DataBase
    {

        public void InsertPlayWay(PlayWay playWay)
        {
            string sql = string.Format(@"INSERT INTO {0} ({1},{2}) VALUES (@{1},@{2})  SELECT IDENT_CURRENT('{0}')",
                PlayWay.TABLENAME, PlayWay.WAYNAME, PlayWay.DESCRIPTION);
            object id = base.ExecuteScalar(sql, new SqlParameter(PlayWay.WAYNAME, playWay.WayName),
                new SqlParameter(PlayWay.DESCRIPTION, playWay.Description));
            playWay.WayId = Convert.ToInt32(id);
        }
        public void UpdatePlayWay(PlayWay playWay)
        {
            string sql = string.Format(@"UPDATE {0} SET {1}=@{1},{2}=@{2} WHERE {3}=@{3}",
                PlayWay.TABLENAME, PlayWay.WAYNAME, PlayWay.DESCRIPTION, PlayWay.WAYID);
            base.ExecuteNonQuery(sql, new SqlParameter(PlayWay.WAYNAME, playWay.WayName),
                new SqlParameter(PlayWay.DESCRIPTION, playWay.Description),
                new SqlParameter(PlayWay.WAYID, playWay.WayId));
        }
        public void DeletePlayWay(int wayId)
        {
            string sql = string.Format(@"DELETE {0} WHERE {1}=@{1} DELETE {2} WHERE {1}=@{1}",
                PlayWay.TABLENAME, PlayWay.WAYID, GamePlayWay.TABLENAME, GamePlayWay.WAYID);
            base.ExecuteNonQuery(sql, new SqlParameter(PlayWay.WAYID, wayId));
        }

        public PlayWay GetPlayWay(int wayId)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1}", PlayWay.TABLENAME, PlayWay.WAYID);
            return base.ExecuteEntity<PlayWay>(sql, new SqlParameter(PlayWay.WAYID, wayId));
        }
        public PlayWay GetPlayWay(string wayName)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1}", PlayWay.TABLENAME, PlayWay.WAYNAME);
            return base.ExecuteEntity<PlayWay>(sql, new SqlParameter(PlayWay.WAYNAME, wayName));
        }
        public IList<PlayWay> GetAllPlayWay()
        {
            string sql = string.Format("SELECT * FROM {0}", PlayWay.TABLENAME);
            return base.ExecuteList<PlayWay>(sql);
        }
    }
}
